﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Views_EmployeesBasic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            StoreListEmpsBasic_LoadData();
        }
    }

    protected void StoreListEmpsBasic_ReadData(object sender, Ext.Net.StoreReadDataEventArgs e)
    {
        StoreListEmpsBasic_LoadData();
    }

    public void StoreListEmpsBasic_LoadData()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {

                StoreListEmpsBasic.DataSource = db.Employees;
                StoreListEmpsBasic.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    protected void RowSelected(object sender, DirectEventArgs e)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = null;
                try
                {
                    rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                    if (rsm.SelectedRow == null)
                    {
                        X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xem và chỉnh sửa thông tin").Show();
                        return;
                    }

                    int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                    Employee emp = db.Employees.Single(t => t.EmpID == empID);

                    DisplayFieldCode.Text = emp.Code;
                    DisplayFieldFullname.Text = emp.FirstName + " " + emp.LastName;
                    DisplayFieldOtherName.Text = emp.NameOther;

                    if (Boolean.Parse(emp.Gender.ToString()))
                        DisplayFieldGender.Text = "Nam";
                    else
                        DisplayFieldGender.Text = "Nữ";
                    DisplayFieldDateOfBirth.Text = DateTime.Parse(emp.DateOfBirth.ToString()).ToString("dd/MM/yyyy");
                    DisplayFieldEthnic.Text = emp.Ethnic;
                    DisplayFieldReligion.Text = emp.Religion;

                    DisplayFieldTelephone.Text = emp.Telephone;
                    DisplayFieldEmail.Text = emp.Email;
                    DisplayFieldPlaceOfBirth.Text = emp.PlaceOfBirth;
                    DisplayFieldHomeTown.Text = emp.HomeTown;
                    DisplayFieldHouseHold.Text = emp.HouseHold;
                    DisplayFieldCurrentAddress.Text = emp.CurrentAddress;

                    DisplayFieldJobCurrent.Text = emp.JobCurrent;
                    DisplayFieldDateOfRecruitment.Text = DateTime.Parse(emp.DateOfRecruitment.ToString()).ToString("dd/MM/yyyy");
                    DisplayFieldOrganOfRecruitment.Text = emp.OrganOfRecruitment;
                    DisplayFieldPosition.Text = emp.Postion.NameOfPost;
                    DisplayFieldTaskIsAssigned.Text = emp.TaskIsAssigned;
                    DisplayFieldDepartManage.Text = "Phòng ban " + emp.Department.NameOfDep + ", Cơ quan " + emp.Department.Organization.NameOfOrgan
                        + ", Xã phường " + emp.Department.Organization.Commune.NameOfComm + ", quận huyện " + emp.Department.Organization.Commune.District.NameOfDist
                        + ", tỉnh thành " + emp.Department.Organization.Commune.District.Province.NameOfProvince;
                    DisplayFieldDepartUse.Text = "Phòng ban " + emp.Department1.NameOfDep + ", Cơ quan " + emp.Department1.Organization.NameOfOrgan
                       + ", Xã phường " + emp.Department1.Organization.Commune.NameOfComm + ", quận huyện " + emp.Department1.Organization.Commune.District.NameOfDist
                       + ", tỉnh thành " + emp.Department1.Organization.Commune.District.Province.NameOfProvince;

                    if (emp.AvatarUrl != null)
                    {
                        ImageAvatarView.ImageUrl = "../Views/Avatars/" + emp.AvatarUrl;
                    }
                    else
                        ImageAvatarView.ImageUrl = "../Views/Avatars/add_user_256.png";
                }
                catch (Exception)
                {
                    X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xem và chỉnh sửa thông tin [Dòng 58]").Show();
                    return;
                }


            }

        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    protected void ToolbarShowAdd_DirectClick(object sender, DirectEventArgs e)
    {
        WindowAddEmpsBasic.Hidden = false;
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                StoreAddPosition.DataSource = db.Postions;
                StoreAddPosition.DataBind();

                List<Province> provList = db.Provinces.ToList<Province>();

                StoreAddManaAddProvinces.DataSource = provList;
                StoreAddManaAddProvinces.DataBind();

                StoreAddManaDistrict.DataSource = new List<District>();
                StoreAddManaDistrict.DataBind();

                StoreAddManaCommune.DataSource = new List<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StoreAddUseProvince.DataSource = provList;
                StoreAddUseProvince.DataBind();

                StoreUseDistrict.DataSource = new List<District>();
                StoreUseDistrict.DataBind();

                StoreAddUseCommune.DataSource = new List<Commune>();
                StoreAddUseCommune.DataBind();

                StoreAddUseOrganization.DataSource = new List<Organization>();
                StoreAddUseOrganization.DataBind();

                StoreAdduseDepartment.DataSource = new List<Department>();
                StoreAdduseDepartment.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    //Lấy dữ liệu cho phần chọn cơ quan quản lí nhân viên trong cửa sổ thêm mới nhân viên
    [DirectMethod]
    public void cbxAddManaProvinces_SelectedChange()
    {
        try
        {
            StoreAddManaCommune.DataSource = new List<Commune>();
            StoreAddManaCommune.DataBind();

            StoreAddManaOrganization.DataSource = new List<Organization>();
            StoreAddManaOrganization.DataBind();

            StoreAddManaDepartment.DataSource = new List<Department>();
            StoreAddManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxAddManaProvince.Value.ToString());

                Province prov = db.Provinces.Single(p => p.ProvID == provID);

                StoreAddManaDistrict.DataSource = prov.Districts.ToList<District>();
                StoreAddManaDistrict.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddManaDistrict_SelectedChange()
    {
        try
        {
            StoreAddManaOrganization.DataSource = new List<Organization>();
            StoreAddManaOrganization.DataBind();

            StoreAddManaDepartment.DataSource = new List<Department>();
            StoreAddManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxAddManaDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreAddManaCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreAddManaCommune.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddManaCommune_SelectedChange()
    {
        try
        {
            StoreAddManaDepartment.DataSource = new List<Department>();
            StoreAddManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxAddManaCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreAddManaOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreAddManaOrganization.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddManaOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxAddManaOrganization.Value.ToString());

                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreAddManaDepartment.DataSource = org.Departments.ToList<Department>();
                StoreAddManaDepartment.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    //Lấy dữ liệu phòng ban cơ quan đang làm việc trong cửa sổ thêm mới nhân viên
    [DirectMethod]
    public void cbxAddUseProvinces_SelectedChange()
    {
        try
        {
            StoreAddUseCommune.DataSource = new List<Commune>();
            StoreAddUseCommune.DataBind();

            StoreAddUseOrganization.DataSource = new List<Organization>();
            StoreAddUseOrganization.DataBind();

            StoreAdduseDepartment.DataSource = new List<Department>();
            StoreAdduseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxAddUseProvince.Value.ToString());

                Province prov = db.Provinces.Single(p => p.ProvID == provID);

                StoreUseDistrict.DataSource = prov.Districts.ToList<District>();
                StoreUseDistrict.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddUseDistrict_SelectedChange()
    {
        try
        {
            StoreAddUseOrganization.DataSource = new List<Organization>();
            StoreAddUseOrganization.DataBind();

            StoreAdduseDepartment.DataSource = new List<Department>();
            StoreAdduseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxAddUseDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreAddUseCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreAddUseCommune.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddUseCommune_SelectedChange()
    {
        try
        {
            StoreAdduseDepartment.DataSource = new List<Department>();
            StoreAdduseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxAddUseCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreAddUseOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreAddUseOrganization.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxAddUseOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxAddUseOrganization.Value.ToString());
                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreAdduseDepartment.DataSource = org.Departments.ToList<Department>();
                StoreAdduseDepartment.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    //Lấy dữ liệu cho phần chọn cơ quan quản lí nhân viên trong cửa sổ cập nhật thông tin cán bộ nhân viên
    [DirectMethod]
    public void cbxUpdateManaProvince_SelectedChange()
    {
        try
        {
            StoreUpdateManaCommune.DataSource = new List<Commune>();
            StoreUpdateManaCommune.DataBind();

            StoreUpdateManaOrganization.DataSource = new List<Organization>();
            StoreUpdateManaOrganization.DataBind();

            StoreUpdateManaDepartment.DataSource = new List<Department>();
            StoreUpdateManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxUpdateManaProvince.Value.ToString());

                Province prov = db.Provinces.Single(p => p.ProvID == provID);

                StoreUpdateManaDistrict.DataSource = prov.Districts.ToList<District>();
                StoreUpdateManaDistrict.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateManaDistrict_SelectedChange()
    {
        try
        {
            StoreUpdateManaOrganization.DataSource = new List<Organization>();
            StoreUpdateManaOrganization.DataBind();

            StoreUpdateManaDepartment.DataSource = new List<Department>();
            StoreUpdateManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxUpdateManaDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreUpdateManaCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreUpdateManaCommune.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateManaCommune_SelectedChange()
    {
        try
        {
            StoreUpdateManaDepartment.DataSource = new List<Department>();
            StoreUpdateManaDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxUpdateManaCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreUpdateManaOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreUpdateManaOrganization.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateManaOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxUpdateManaOrganization.Value.ToString());

                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreUpdateManaDepartment.DataSource = org.Departments.ToList<Department>();
                StoreUpdateManaDepartment.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    //Lấy dữ liệu phòng ban cơ quan đang làm việc trong cửa cập nhật thông tin cán bộ nhân viên
    [DirectMethod]
    public void cbxUpdateUseProvince_SelectedChange()
    {
        try
        {
            StoreUpdateUseCommune.DataSource = new List<Commune>();
            StoreUpdateUseCommune.DataBind();

            StoreUpdateUseOrganization.DataSource = new List<Organization>();
            StoreUpdateUseOrganization.DataBind();

            StoreUpdateUseDepartment.DataSource = new List<Department>();
            StoreUpdateUseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxUpdateUseProvince.Value.ToString());

                Province prov = db.Provinces.Single(p => p.ProvID == provID);

                StoreUpdateUseDistrict.DataSource = prov.Districts.ToList<District>();
                StoreUpdateUseDistrict.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateUseDistrict_SelectedChange()
    {
        try
        {
            StoreUpdateUseOrganization.DataSource = new List<Organization>();
            StoreUpdateUseOrganization.DataBind();

            StoreUpdateUseDepartment.DataSource = new List<Department>();
            StoreUpdateUseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxUpdateUseDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreUpdateUseCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreUpdateUseCommune.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateUseCommune_SelectedChange()
    {
        try
        {
            StoreUpdateUseDepartment.DataSource = new List<Department>();
            StoreUpdateUseDepartment.DataBind();

            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxUpdateUseCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreUpdateUseOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreUpdateUseOrganization.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void cbxUpdateUseOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxUpdateUseOrganization.Value.ToString());
                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreUpdateUseDepartment.DataSource = org.Departments.ToList<Department>();
                StoreUpdateUseDepartment.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }

    [DirectMethod]
    public void Employee_Add()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                string empCode = TextAddCode.Text.Trim();
                int posID = Int32.Parse(cbxAddPosition.Value.ToString());
                bool gender = false;
                if (RadioAddMale.Checked)
                {
                    gender = true;
                }
                int deparManatID = Int32.Parse(cbxAddManaDepartment.Value.ToString());
                int departUseID = Int32.Parse(cbxAdduseDepartment.Value.ToString());

                List<Employee> empList = db.Employees.ToList<Employee>();
                foreach (var item in empList)
                {
                    if (item.Code.ToLower().Equals(empCode.ToLower()))
                    {
                        X.Msg.Alert("Thông báo", "Số hiệu CBCC này đã được sử dụng, hãy nhập lại").Show();
                        return;
                    }
                }

                Employee emp = new Employee()
                {
                    Code = empCode,
                    FirstName = TextAddFirstName.Text.Trim(),
                    LastName = TextAddLastName.Text.Trim(),
                    NameOther = TextAddOtherName.Text.Trim(),
                    Email = TextAddEmail.Text.Trim(),
                    Telephone = TextAddPhone.Text.Trim(),
                    JobCurrent = TextAddJob.Text.Trim(),
                    DateOfRecruitment = DateTime.Parse(TextAddDateOfRecruitment.Text.Trim()),
                    OrganOfRecruitment = TextAddOrganOfRecruitment.Text.Trim(),
                    PosID = posID,
                    TaskIsAssigned = TextAddTaskIsAssigned.Text.Trim(),
                    DateOfBirth = DateTime.Parse(TextAddDateOfBirth.Text),
                    Gender = gender,
                    Ethnic = TextAddEthnic.Text.Trim(),
                    Religion = TextAddReligion.Text.Trim(),
                    PlaceOfBirth = TextAddPlaceOfBirth.Text.Trim(),
                    HomeTown = TextAddHomeTown.Text.Trim(),
                    HouseHold = TextAddHouseHold.Text.Trim(),
                    CurrentAddress = TextAddCurrentAddress.Text.Trim(),
                    DepManagerID = deparManatID,
                    DepUsingID = departUseID,
                    StatusOfEmp = CheckboxAddStatus.Checked,
                    AvatarUrl = "add_user_256.png",
                };

                db.Employees.Add(emp);
                db.SaveChanges();

                // Thêm mới Profile cho Emplloyees
                Profily proNew = new Profily()
                {
                    EmpID = db.Employees.Single(e => e.Code == empCode).EmpID
                };
                db.Profilies.Add(proNew);
                db.SaveChanges();
                StoreListEmpsBasic_LoadData();
                X.Msg.Alert("Thông báo", "Thêm mới nhân viên thành công").Show();
                WindowAddEmpsBasic.Hidden = true;
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void Employee_Update()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int posID = Int32.Parse(cbxUpdatePosition.Value.ToString());
                int depManaID = Int32.Parse(cbxUpdateManaDepartment.Value.ToString());
                int depUseID = Int32.Parse(cbxUpdateUseDepartment.Value.ToString());
                RowSelectionModel rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                Employee emp = db.Employees.Single(t => t.EmpID == empID);

                string empCode = TextUpdateCode.Text.Trim();
                try
                {
                    Employee checkEmp = db.Employees.Single(e => e.Code == empCode && e.EmpID != emp.EmpID);
                    X.Msg.Alert("Thông báo", "Số hiệu CBCC đã được sử dụng hãy nhập lại").Show();
                    return;
                }
                catch (Exception)
                {
                }

                emp.Code = TextUpdateCode.Text.Trim();
                emp.FirstName = TextUpdateFirstName.Text.Trim();
                emp.LastName = TextUpdateLastName.Text.Trim();
                emp.NameOther = TextUpdateOtherName.Text.Trim();
                emp.Email = TextUpdateEmail.Text.Trim();
                emp.Telephone = TextUpdateTelephone.Text.Trim();
                emp.JobCurrent = TextUpdateJobCurrent.Text.Trim();
                emp.DateOfRecruitment = DateTime.Parse(TextUpdateDateOfRecruitment.Text.Trim());
                emp.OrganOfRecruitment = TextUpdateOrganOfRecruitment.Text.Trim();
                emp.PosID = posID;
                emp.TaskIsAssigned = TextUpdateTaskIsAssigned.Text.Trim();
                emp.StatusOfEmp = CheckboxUpdateStatus.Checked;

                emp.DateOfBirth = DateTime.Parse(TextUpdateDateOfBirth.Text.Trim());
                emp.Gender = RadioUpdateMale.Checked;
                emp.Ethnic = TextUpdateEthnic.Text.Trim();
                emp.Religion = TextUpdateReligion.Text.Trim();
                emp.PlaceOfBirth = TextUpdatePlaceOfBirth.Text.Trim();
                emp.HomeTown = TextUpdateHomeTown.Text.Trim();
                emp.HouseHold = TextUpdateHouseHold.Text.Trim();
                emp.CurrentAddress = TextUpdateCurrentAddress.Text.Trim();

                emp.DepManagerID = depManaID;
                emp.DepUsingID = depUseID;

                db.SaveChanges();
                StoreListEmpsBasic_LoadData();
                WindowUpdateEmpsBasic.Hidden = true;
                X.Msg.Alert("Thông báo", "Cập nhật thông tin cán bộ nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    protected void ButtonAddClose_DirectClick(object sender, DirectEventArgs e)
    {
        X.Msg.Confirm("Thông báo ", String.Format("Bạn có muốn đóng cửa  sổ thêm mới cán bộ nhân viên không?"),
                        new MessageBoxButtonsConfig()
                        {
                            Yes = new MessageBoxButtonConfig
                            {
                                Text = "Đóng",
                                Handler = "#{WindowAddEmpsBasic}.close()"
                            },
                            No = new MessageBoxButtonConfig
                            {
                                Text = "Hủy bỏ"
                            }
                        }).Show();
    }

    protected void ToolbarShowUpdate_DirectClick(object sender, DirectEventArgs e)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = null;
                try
                {

                    rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                    if (rsm.SelectedRow == null)
                    {
                        X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xem và chỉnh sửa thông tin").Show();
                        return;
                    }


                    int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                    Employee emp = db.Employees.Single(t => t.EmpID == empID);

                    StoreUpdatePosition.DataSource = db.Postions;
                    StoreUpdatePosition.DataBind();
                    cbxUpdatePosition.Value = emp.PosID;

                    List<Province> provList = db.Provinces.ToList<Province>();

                    StoreUpdateManaProvince.DataSource = provList;
                    StoreUpdateManaProvince.DataBind();
                    cbxUpdateManaProvince.Value = emp.Department.Organization.Commune.District.ProvID;

                    StoreUpdateManaDistrict.DataSource = emp.Department.Organization.Commune.District.Province.Districts;
                    StoreUpdateManaDistrict.DataBind();
                    cbxUpdateManaDistrict.Value = emp.Department.Organization.Commune.DistID;

                    StoreUpdateManaCommune.DataSource = emp.Department.Organization.Commune.District.Communes;
                    StoreUpdateManaCommune.DataBind();
                    cbxUpdateManaCommune.Value = emp.Department.Organization.ComID;

                    StoreUpdateManaOrganization.DataSource = emp.Department.Organization.Commune.Organizations;
                    StoreUpdateManaOrganization.DataBind();
                    cbxUpdateManaOrganization.Value = emp.Department.OrgID;

                    StoreUpdateManaDepartment.DataSource = emp.Department.Organization.Departments;
                    StoreUpdateManaDepartment.DataBind();
                    cbxUpdateManaDepartment.Value = emp.DepManagerID;

                    StoreUpdateUseProvince.DataSource = provList;
                    StoreUpdateUseProvince.DataBind();
                    cbxUpdateUseProvince.Value = emp.Department1.Organization.Commune.District.ProvID;

                    StoreUpdateUseDistrict.DataSource = emp.Department1.Organization.Commune.District.Province.Districts; ;
                    StoreUpdateUseDistrict.DataBind();
                    cbxUpdateUseDistrict.Value = emp.Department1.Organization.Commune.ComID;

                    StoreUpdateUseCommune.DataSource = emp.Department1.Organization.Commune.District.Communes;
                    StoreUpdateUseCommune.DataBind();
                    cbxUpdateUseCommune.Value = emp.Department1.Organization.ComID;

                    StoreUpdateUseOrganization.DataSource = emp.Department1.Organization.Commune.Organizations;
                    StoreUpdateUseOrganization.DataBind();
                    cbxUpdateUseOrganization.Value = emp.Department1.OrgID;

                    StoreUpdateUseDepartment.DataSource = emp.Department1.Organization.Departments;
                    StoreUpdateUseDepartment.DataBind();
                    cbxUpdateUseDepartment.Value = emp.DepUsingID;

                    TextUpdateCode.Text = emp.Code;
                    TextUpdateFirstName.Text = emp.FirstName;
                    TextUpdateLastName.Text = emp.LastName;
                    TextUpdateOtherName.Text = emp.NameOther;
                    TextUpdateEmail.Text = emp.Email;
                    TextUpdateTelephone.Text = emp.Telephone;
                    TextUpdateJobCurrent.Text = emp.JobCurrent;
                    TextUpdateDateOfRecruitment.Text = DateTime.Parse(emp.DateOfRecruitment.ToString()).ToString("dd/MM/yyyy");
                    TextUpdateOrganOfRecruitment.Text = emp.OrganOfRecruitment;
                    TextUpdateTaskIsAssigned.Text = emp.TaskIsAssigned;
                    TextUpdateDateOfBirth.Text = DateTime.Parse(emp.DateOfBirth.ToString()).ToString("dd/MM/yyyy");
                    TextUpdateEthnic.Text = emp.Ethnic;
                    TextUpdateReligion.Text = emp.Religion;
                    TextUpdatePlaceOfBirth.Text = emp.PlaceOfBirth;
                    TextUpdateHomeTown.Text = emp.HomeTown;
                    TextUpdateHouseHold.Text = emp.HouseHold;
                    TextUpdateCurrentAddress.Text = emp.CurrentAddress;
                    CheckboxUpdateStatus.Checked = Boolean.Parse(emp.StatusOfEmp.ToString());
                    if (Boolean.Parse(emp.Gender.ToString()))
                        RadioUpdateMale.Checked = true;
                    else
                        RadioUpdateFemale.Checked = true;
                    WindowUpdateEmpsBasic.Hidden = false;
                }
                catch (Exception)
                {
                    X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xem và chỉnh sửa thông tin [Dòng 464]").Show();
                    return;
                }
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void Employee_QuickEdit(int empID)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                Employee emp = db.Employees.Single(t => t.EmpID == empID);

                StoreUpdatePosition.DataSource = db.Postions;
                StoreUpdatePosition.DataBind();
                cbxUpdatePosition.Value = emp.PosID;

                List<Province> provList = db.Provinces.ToList<Province>();

                StoreUpdateManaProvince.DataSource = provList;
                StoreUpdateManaProvince.DataBind();
                cbxUpdateManaProvince.Value = emp.Department.Organization.Commune.District.ProvID;

                StoreUpdateManaDistrict.DataSource = emp.Department.Organization.Commune.District.Province.Districts;
                StoreUpdateManaDistrict.DataBind();
                cbxUpdateManaDistrict.Value = emp.Department.Organization.Commune.DistID;

                StoreUpdateManaCommune.DataSource = emp.Department.Organization.Commune.District.Communes;
                StoreUpdateManaCommune.DataBind();
                cbxUpdateManaCommune.Value = emp.Department.Organization.ComID;

                StoreUpdateManaOrganization.DataSource = emp.Department.Organization.Commune.Organizations;
                StoreUpdateManaOrganization.DataBind();
                cbxUpdateManaOrganization.Value = emp.Department.OrgID;

                StoreUpdateManaDepartment.DataSource = emp.Department.Organization.Departments;
                StoreUpdateManaDepartment.DataBind();
                cbxUpdateManaDepartment.Value = emp.DepManagerID;

                StoreUpdateUseProvince.DataSource = provList;
                StoreUpdateUseProvince.DataBind();
                cbxUpdateUseProvince.Value = emp.Department1.Organization.Commune.District.ProvID;

                StoreUpdateUseDistrict.DataSource = emp.Department1.Organization.Commune.District.Province.Districts; ;
                StoreUpdateUseDistrict.DataBind();
                cbxUpdateUseDistrict.Value = emp.Department1.Organization.Commune.ComID;

                StoreUpdateUseCommune.DataSource = emp.Department1.Organization.Commune.District.Communes;
                StoreUpdateUseCommune.DataBind();
                cbxUpdateUseCommune.Value = emp.Department1.Organization.ComID;

                StoreUpdateUseOrganization.DataSource = emp.Department1.Organization.Commune.Organizations;
                StoreUpdateUseOrganization.DataBind();
                cbxUpdateUseOrganization.Value = emp.Department1.OrgID;

                StoreUpdateUseDepartment.DataSource = emp.Department1.Organization.Departments;
                StoreUpdateUseDepartment.DataBind();
                cbxUpdateUseDepartment.Value = emp.DepUsingID;

                TextUpdateCode.Text = emp.Code;
                TextUpdateFirstName.Text = emp.FirstName;
                TextUpdateLastName.Text = emp.LastName;
                TextUpdateOtherName.Text = emp.NameOther;
                TextUpdateEmail.Text = emp.Email;
                TextUpdateTelephone.Text = emp.Telephone;
                TextUpdateJobCurrent.Text = emp.JobCurrent;
                TextUpdateDateOfRecruitment.Text = DateTime.Parse(emp.DateOfRecruitment.ToString()).ToString("dd/MM/yyyy");
                TextUpdateOrganOfRecruitment.Text = emp.OrganOfRecruitment;
                TextUpdateTaskIsAssigned.Text = emp.TaskIsAssigned;
                TextUpdateDateOfBirth.Text = DateTime.Parse(emp.DateOfBirth.ToString()).ToString("dd/MM/yyyy");
                TextUpdateEthnic.Text = emp.Ethnic;
                TextUpdateReligion.Text = emp.Religion;
                TextUpdatePlaceOfBirth.Text = emp.PlaceOfBirth;
                TextUpdateHomeTown.Text = emp.HomeTown;
                TextUpdateHouseHold.Text = emp.HouseHold;
                TextUpdateCurrentAddress.Text = emp.CurrentAddress;
                CheckboxUpdateStatus.Checked = Boolean.Parse(emp.StatusOfEmp.ToString());
                if (Boolean.Parse(emp.Gender.ToString()))
                    RadioUpdateMale.Checked = true;
                else
                    RadioUpdateFemale.Checked = true;

                WindowUpdateEmpsBasic.Hidden = false;
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xem và chỉnh sửa thông tin [Dòng 464]").Show();
            return;
        }
    }

    protected void ToolbarShowDelete_DirectClick(object sender, DirectEventArgs e)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = null;
                try
                {

                    rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                    if (rsm.SelectedRow == null)
                    {
                        X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xóa").Show();
                        return;
                    }
                    int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                    Employee emp = db.Employees.Single(t => t.EmpID == empID);

                    X.Msg.Confirm("Hệ thống", String.Format("Bạn có muốn xóa cán bộ nhân viên đã chọn ? [{0}]", emp.FirstName + " " + emp.LastName),
                        new MessageBoxButtonsConfig()
                        {
                            Yes = new MessageBoxButtonConfig
                            {
                                Text = "Xóa",
                                Handler = "App.direct.DoYes_DeleteEmployee(" + empID + ");"
                            },
                            No = new MessageBoxButtonConfig
                            {
                                Text = "Không xóa"
                            },
                        }).Show();
                }
                catch (Exception)
                {
                    X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xóa").Show();
                    return;
                }
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void DoYes_DeleteEmployee(string empID)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int id = Int32.Parse(empID);
                Employee emp = db.Employees.Single(e => e.EmpID == id);

                //Xóa profile của Employee
                List<Profily> listProfile = emp.Profilies.ToList<Profily>();
                foreach (var item in listProfile)
                {
                    db.Profilies.Remove(item);
                }

                //Xóa tất cả TranningForestings của Employee
                List<TrainingFostering> trainList = emp.TrainingFosterings.ToList<TrainingFostering>();
                foreach (var item in trainList)
                {
                    db.TrainingFosterings.Remove(item);
                }

                //Xóa tất cả EducationsForEmps của employee
                List<EducationsForEmp> eduList = emp.EducationsForEmps.ToList<EducationsForEmp>();
                foreach (var item in eduList)
                {
                    db.EducationsForEmps.Remove(item);
                }

                //Xóa tất cả FamilyRelationships của employee
                List<FamilyRelationship> familyList = emp.FamilyRelationships.ToList<FamilyRelationship>();
                foreach (var item in familyList)
                {
                    db.FamilyRelationships.Remove(item);
                }

                //Xóa tất cả EmployeesSalaryLevel của employee
                List<EmployeesSalaryLevel> salarylevalList = emp.EmployeesSalaryLevels.ToList<EmployeesSalaryLevel>();
                foreach (var item in salarylevalList)
                {
                    db.EmployeesSalaryLevels.Remove(item);
                }

                db.Employees.Remove(emp);
                db.SaveChanges();
                StoreListEmpsBasic_LoadData();
                ResetForm_ViewDetail();
                X.Msg.Alert("Thông báo", "Xóa nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo hệ thống", "Xóa nhân viên thành công").Show();
        }
    }


    protected void ToolbarShowUpload_DirectClick(object sender, DirectEventArgs e)
    {

        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = null;
                try
                {

                    rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                    if (rsm.SelectedRow == null)
                    {
                        X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để thay ảnh đại diện").Show();
                        return;
                    }
                    int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                    Employee emp = db.Employees.Single(t => t.EmpID == empID);

                    WindowUpdateAvatar.Hidden = false;

                    if (emp.AvatarUrl == null)
                    {
                        ImageAvatar.ImageUrl = "../Views/Avatars/add_user_256.png";
                    }
                    else
                    {
                        ImageAvatar.ImageUrl = "../Views/Avatars/" + emp.AvatarUrl;
                    }
                }
                catch (Exception)
                {
                    //X.Msg.Alert("Thông báo", "Bạn chưa chọn cán bộ nhân viên để xóa").Show();
                    return;
                }
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void LoadImage_FileUpload()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = GridPanelListEmpsBasic.SelectionModel.Primary as RowSelectionModel;
                int empID = Int32.Parse(rsm.SelectedRow.RecordID);
                Employee emp = db.Employees.Single(t => t.EmpID == empID);

                string pathUpload = Server.MapPath("Avatars");
                string fileName = FileUploadAvatar.PostedFile.FileName.ToString();
                string newNameAvatar = UltisCode.CreateNewImageName(fileName, 30);

                //Xóa ảnh cũ
                if (emp.AvatarUrl != null && emp.AvatarUrl != "add_user_256.png")
                {
                    File.Delete(pathUpload + @"\" + emp.AvatarUrl);
                }

                pathUpload = pathUpload + @"\" + newNameAvatar;

                FileUploadAvatar.PostedFile.SaveAs(pathUpload);
                emp.AvatarUrl = newNameAvatar;
                db.SaveChanges();

                ImageAvatar.ImageUrl = "../Views/Avatars/" + newNameAvatar;

                WindowUpdateAvatar.Hidden = true;
                ResetForm_ViewDetail();
                StoreListEmpsBasic_LoadData();

                X.Msg.Alert("Thông báo", "Cập nhật ảnh đại diện cho nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo hệ thống", "Hệ thống đang bận xin bạn hãy thử lại sau").Show();
        }
    }

    protected void ButtonUpdateClose_DirectClick(object sender, DirectEventArgs e)
    {
        X.Msg.Confirm("Thông báo ", String.Format("Bạn có muốn đóng cửa cập nhật thông tin cán bộ nhân viên không?"),
                        new MessageBoxButtonsConfig()
                        {
                            Yes = new MessageBoxButtonConfig
                            {
                                Text = "Đóng",
                                Handler = "#{WindowUpdateEmpsBasic}.close();"
                            },
                            No = new MessageBoxButtonConfig
                            {
                                Text = "Hủy bỏ"
                            }
                        }).Show();
    }

    public void ResetForm_ViewDetail()
    {
        ImageAvatarView.ImageUrl = "../Views/Avatars/add_user_256.png";
        DisplayFieldCode.Text = "";
        DisplayFieldFullname.Text = "";
        DisplayFieldOtherName.Text = "";
        DisplayFieldGender.Text = "";
        DisplayFieldDateOfBirth.Text = "";
        DisplayFieldEthnic.Text = "";
        DisplayFieldReligion.Text = "";
        DisplayFieldTelephone.Text = "";
        DisplayFieldEmail.Text = "";
        DisplayFieldPlaceOfBirth.Text = "";
        DisplayFieldHomeTown.Text = "";
        DisplayFieldHouseHold.Text = "";
        DisplayFieldCurrentAddress.Text = "";
        DisplayFieldJobCurrent.Text = "";
        DisplayFieldDateOfRecruitment.Text = "";
        DisplayFieldOrganOfRecruitment.Text = "";
        DisplayFieldPosition.Text = "";
        DisplayFieldTaskIsAssigned.Text = "";
        DisplayFieldDepartManage.Text = "";
        DisplayFieldDepartUse.Text = "";
    }

    [DirectMethod]
    public void Employee_ViewDefailt(int empID)
    {
        Response.Redirect("Employees.aspx?emp=" + empID);
    }
}